package e_stack;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:周次煜
 * Date: 2024-03-30
 * Time：21:58
 */
public class EMyStack<E> implements EIMyStack<E>{
    // 定义一个栈
    public Object []elem;

    public EMyStack() {
        this.usesize = 0;

        this.elem =new Object[SIZEMAX];
    }


    // 现有栈元素大小
    public  int usesize;



    // 入栈
    @Override
    public void push(E val) {

        if (isFull()) {
            this.elem= Arrays.copyOf(elem,2*elem.length);
        }

        elem[usesize]=val;
        usesize++;
    }



    // 去除栈顶元素
    @Override
    public E pop() {
        if (isEmpty()) {
            return null;
        }

        E ret=(E) elem[usesize-1];
         elem[usesize]=null;
        usesize--;
        return ret;
    }


    // 喵一眼栈顶元素
    @Override
    public E peek() {
        if (isEmpty()) {
            return null;
        }


        return (E) elem[usesize-1];
    }



    // 检查栈是否满
    @Override
    public boolean isFull() {
        return usesize==size();
    }


    // 坚持是否空
    @Override
    public boolean isEmpty() {
        return usesize==0;
    }


    // 返回栈的大小
    @Override
    public int size() {
        return elem.length;
    }

}
